/**
 * Created by wujo5 on 1/3/2018.
 */
var express = require('express');
var userCtrl = require('../controller/userController');
var router = express.Router();
var app = express();
// app.all('*', function (req, res, next) {
//   res.header('Access-Control-Allow-Origin', '*');
//   res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
//   res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
//   if (req.method == 'OPTIONS') {
//     res.send(200);
//     /*让options请求快速返回*/
//   } else {
//     next();
//   }
// });
// var cors = require('cors');
// app.use(cors())
// app.post('/users/login',function (req,res) {
//   var username = req.body.username;
//   var password = req.body.password;
//   userCtrl.login(username,password,function(succ){
//     if(succ.resultCode=="200") {
//       var user = {username: succ.data.username};
//       req.session.user = user;
//       res.send(succ);
//     }
//   },function (fail) {
//     res.send(fail);
//   })
// });
router.post('/login',function (req,res) {
  // console.log(JSON.stringify(req.body));
  // res.header('Access-Control-Allow-Origin', '*');
  // res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  // res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
  var username = req.body.username;
  var password = req.body.password;
  userCtrl.login(username,password,function(succ){
    if(succ.resultCode=="200") {
      var user = {username: succ.data};
      req.session.user = user;
      res.send(succ);
    }
  },function (fail) {
    res.send(fail);
  })
});

router.post('/logout',function (req,res) {
  req.session.user = null;
  var resJson = {
    resultcode: "200",
    reason: "success",
    error_code: "200",
    data:"logout success"
  };
  res.send(resJson);
});
module.exports = router;
